package com.springboot.jdk.lock;

import java.util.concurrent.locks.LockSupport;

/**
 * @author: zr
 * @date: 2021-10-01
 */
public class ParkDemo {

    public static void main(String[] args) {
        Thread thread1 = new Thread() {

            @Override
            public void run() {
                System.out.println("线程1被挂起，进入阻塞");
                LockSupport.park(this);
                System.out.println("线程1被唤醒之后执行的操作");
            }
        };
        thread1.start();


        Thread thread2 = new Thread() {

            @Override
            public void run() {
                System.out.println("线程2被挂起，进入阻塞");
                LockSupport.park(thread1);
                System.out.println("线程2被唤醒之后执行的操作");
            }
        };
        thread2.start();


        new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    System.out.println("等待" + (i) + "秒");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                System.out.println("尝试唤醒挂起的线程1");
                LockSupport.unpark(thread1);

                System.out.println("尝试唤醒挂起的线程2");
                LockSupport.unpark(thread2);
            }
        }.start();
    }
}
